<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools :show-before="false">
        <template v-slot:after>
          <el-button type="primary" size="small" @click="onClickAdd(1,'0')">添加权限</el-button>
        </template>
      </PageTools>
      <el-table ref="table" :data="tableData" row-key="id">
        <el-table-column label="名称">
          <template slot-scope="scope">
            <i v-if="scope.row.type === 1" class="el-icon-folder-opened" @click="onExpand(scope.row)" />
            <i v-else class="el-icon-view" />
            <span> {{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="onClickAdd(2,row.id)">添加</el-button>
            <el-button type="text" @click="onEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="title"
      :visible="updatePermissionDialog"
      width="50%"
      @close="onClose"
    >
      <el-form ref="form" :model="addOrEditForm" :rules="rules" label-width="80px">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="addOrEditForm.name" />
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model="addOrEditForm.code" />
        </el-form-item>
        <el-form-item prop="description" label="权限描述">
          <el-input v-model="addOrEditForm.description" />
        </el-form-item>
        <el-form-item label="权限开启">
          <el-switch
            v-model="addOrEditForm.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionInfo, delPermission, updatePermission } from '@/api/permission'
import { transListToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      tableData: [],
      updatePermissionDialog: false,
      addOrEditForm: {
        description: '',
        code: '',
        name: '',
        enVisible: true,
        type: 1
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.addOrEditForm.id ? '编辑' : '添加'
    }
  },

  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      this.tableData = transListToTree('0', res)
    },
    onExpand(row) {
      row.isExpand = !row.isExpand
      this.$refs.table.toggleRowExpansion(row, row.isExpand)
    },
    async onSave() {
      try {
        await this.$refs.form.validate()
        if (this.addOrEditForm.id) {
          await updatePermission(this.addOrEditForm)
          this.$message.success('编辑成功')
        } else {
          await addPermission(this.addOrEditForm)
          this.$message.success('添加成功')
        }
      } catch (error) {
        this.$message.error('添加失败')
      }
      this.loadPermissionList()
      this.onClose()
    },
    onClose() {
      this.updatePermissionDialog = false
      this.addOrEditForm = {
        description: '',
        code: '',
        name: '',
        enVisible: true,
        type: 1
      }
      this.$refs.form.resetFields()
    },
    onClickAdd(type, pid) {
      this.updatePermissionDialog = true
      this.addOrEditForm.type = type
      this.addOrEditForm.pid = pid
    },
    async delPermission(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.loadPermissionList()
    },
    async onEdit(id) {
      this.updatePermissionDialog = true
      const res = await getPermissionInfo(id)
      this.addOrEditForm = res
      this.loadPermissionList()
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-table__expand-icon{
  display: none !important;
}
</style>
